package cn.lytcom.architecture.api;

import android.os.Build;

import java.io.IOException;

import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by lytcom on 2017/5/7.
 */

public class HeaderRequestInterceptor implements Interceptor {

    private static final String HEADER_ACCEPT = "Accept";
    private static final String HEADER_APPLICATION_JSON = "application/json";
    private static final String HEADER_ANDROID = "Android";
    private static final String HEADER_OS_VERSION = "os-version";
    private static final String HEADER_PLATFORM = "platform";
    private static final String HEADER_DEVICE_ID = "device-id";
    private static final String HEADER_DEVICE_MANUFACTURER = "device-manufacturer";
    private static final String HEADER_DEVICE_MODEL = "device-model";

    @Override
    public Response intercept(Chain chain) throws IOException {
        Request request = chain.request();
        Request newRequest = rewriteRequestWithLatestHeader(request);
        return chain.proceed(newRequest);
    }

    private static Request rewriteRequestWithLatestHeader(Request request) {
        Request.Builder builder = request.newBuilder();
        builder.header(HEADER_ACCEPT, HEADER_APPLICATION_JSON);
        builder.header(HEADER_PLATFORM, HEADER_ANDROID);
        builder.header(HEADER_OS_VERSION, Build.VERSION.RELEASE);
        builder.header(HEADER_DEVICE_ID, Build.ID);
        builder.header(HEADER_DEVICE_MANUFACTURER, Build.MANUFACTURER);
        builder.header(HEADER_DEVICE_MODEL, Build.MODEL);
        return builder.build();
    }
}
